const db = require('../db/dbconfig');

// 获取所有角色信息
const getroles = async(req, res) => {
    let { keyword, pagenum, pagesize } = req.query;
    let sql = `select r.id,r.rolename,r.cover,roletype.roletypename,r.gender,r.game,r.description,game.gamename from role r
    INNER JOIN roletype on r.roletype = roletype.id
    INNER JOIN game on r.game = game.id`,
        data = null,
        msg = '请求成功！',
        status = 200,
        total = 0
    let count_sql = `select count(*) 'total' from role`
    try {
        if (pagenum && pagesize) {
            if (keyword) {
                sql += ` where rolename like '%${keyword}%'`
                count_sql += ` where rolename like '%${keyword}%'`
            }
            sql += ` limit ${(pagenum-1) * pagesize},${pagesize}`
            data = await db.sqlconnection(sql)
            const count_result = await db.sqlconnection(count_sql)
            total = count_result[0].total
        } else {
            status = 400
            msg = '参数必填'
        }
    } catch (error) {
        msg = `请求发生错误:${error}`
        status = 400
    } finally {
        res.status(status).send({
            data,
            total,
            meta: {
                status,
                msg
            }
        })
    }
}

// 添加角色
const addrole = async(req, res) => {
    let { rolename, cover, roletype, gender, description, game } = req.body,
        status = 200
    insert_sql = `insert into role (rolename, cover, roletype, gender, description, game)
        values ('${rolename}','${cover}','${roletype}','${gender}','${description?description:null}','${game}')`,
        msg = '注册成功',
        data = null
    if (rolename && cover && roletype && gender && game) { // 判断输入是否为空
        let select_sql = `select * from role where rolename = '${rolename}'`
        const result = await db.sqlconnection(select_sql);
        if (result.length === 0) { // 判断用户名是否已经被注册
            let add_result = await db.sqlconnection(insert_sql)
            if (add_result.affectedRows <= 0) { // 判断新增sql语句是否添加成功
                msg = '角色添加失败'
                status = 400
            }
        } else {
            status = 400
            msg = '角色已经存在'
        }
    } else {
        status = 400
        msg = '参数必填'
    }
    res.status(status).send({
        data,
        meta: {
            status,
            msg
        }
    })
}

// 删除用户信息
const deleterole = async(req, res) => {
    let { id } = req.query,
        sql = `delete from role where id = '${id}'`,
        msg = '删除角色成功',
        status = 200;
    try {
        const result = await db.sqlconnection(sql)
        if (result.affectedRows <= 0) { // 根据受影响行数来判断是否删除成功
            status = 400
            msg = '删除角色信息失败'
        }
    } catch (error) {
        status = 400
        msg = `执行出错`
    } finally {
        res.status(status).send({
            msg,
            status
        })
    }
}

// 修改用户信息
const updaterole = async(req, res) => {
    let { id, rolename, cover, roletype, gender, description, game } = req.body,
        status = 200
    update_sql = `update role set rolename = ?,cover = ?,roletype = ?,gender = ?,description = ?,game = ? where id = ?`,
        msg = '修改信息成功!'
    if (rolename && cover && roletype && gender && game) {
        const result = await db.sqlconnection(update_sql, [rolename, cover, roletype, gender, description ? description : null, game, id]);
        if (result.affectedRows <= 0) {
            status = 400
            msg = '信息修改失败!'
        }
    } else {
        status = 400
        msg = '参数必填'
    }
    res.status(status).send({
        status,
        msg
    })
}

// 查询单个角色信息
const get_onerole = async(req, res) => {
    let { id } = req.query,
        status = 200
    let sql = `select r.id,r.rolename,r.cover,rt.roletypename,r.gender,r.description,g.gamename  from role r 
    INNER JOIN roletype rt on r.roletype = rt.id
    INNER JOIN game g on r.game = g.id
    where r.id=${id}`,
        data = null,
        msg = '请求成功！'
    try {
        if (id && id > 0) {
            data = await db.sqlconnection(sql)
        } else {
            status = 400
            msg = '必填参数'
        }
    } catch (error) {
        msg = `请求出错了,原因:${error}`
        status = 400
    } finally {
        res.send({
            data,
            meta: {
                status,
                msg
            }
        })
    }
};



module.exports = {
    getroles,
    addrole,
    deleterole,
    updaterole,
    get_onerole
}